﻿using DevExpressWebApplication1.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DevExpressWebApplication1.Module.PD
{
    public partial class Product : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void cbAddEdit_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
        {
            string[] split = e.Parameter.Split(';');
            if (split[0] == "Edit")
            {
                txtName.Text = gvProduct.GetRowValues(int.Parse(split[1].ToString()), "category_name").ToString();
                txtCode.Text = gvProduct.GetRowValues(int.Parse(split[1].ToString()), "code").ToString();
                txtBuyPrice.Text = gvProduct.GetRowValues(int.Parse(split[1].ToString()), "buy_price").ToString();
                txtDescription.Text = gvProduct.GetRowValues(int.Parse(split[1].ToString()), "description").ToString();
                txtSellPrice.Text = gvProduct.GetRowValues(int.Parse(split[1].ToString()), "price").ToString();
                ControlUtils.SetCBBValue(cbbCategory, gvProduct.GetRowValues(int.Parse(split[1].ToString()), "category_id").ToString());
                hdfProductID.Set("ProId", gvProduct.GetRowValues(int.Parse(split[1].ToString()), "product_id").ToString());
            }
         
        }

        protected void gvProduct_CustomCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCustomCallbackEventArgs e)
        {
            gvProduct.DataBind();
        }

        protected void cpReloadGrid_Callback(object source, DevExpress.Web.ASPxCallback.CallbackEventArgs e)
        {
            int productId = int.Parse(hdfProductID.Get("ProId").ToString());
            if (productId > 0)
            {
                try
                {
                    ProductUtils.Update(productId, txtName.Text, txtCode.Text, txtBuyPrice.Text, txtSellPrice.Text, txtDescription.Text, ControlUtils.GetCBBValue(cbbCategory));
                    //gvProduct.DataBind();
                    e.Result= "Success";
                }
                catch (Exception ex)
                {
                    Console.Write("Update Customer fails : " + ex.Message);
                    e.Result= "Update Fail" + ex.Message;
                }
            }
            else
            {
                try
                {
                    ProductUtils.Insert(txtName.Text, txtCode.Text, txtBuyPrice.Text, txtSellPrice.Text, txtDescription.Text, ControlUtils.GetCBBValue(cbbCategory));
                    //gvProduct.DataBind();
                    e.Result="Success";
                }
                catch (Exception ex)
                {
                    Console.Write(ex);
                    e.Result="Insert Fail" + ex.Message;
                }
            }
        }
    }
}